package com.bing.task.v1;

import com.bing.entity.Account;
import com.bing.services.AccountServices;
import com.bing.services.AnswerServices;
import com.bing.services.QuestionServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

/**
 * Created by kaibb on 15-11-23.
 */
public class UpdateQuestionDb implements Runnable {
    private QuestionResult questionResult;
    private QuestionServices questionServices;
    private AccountServices accountServices;
    private AnswerServices answerServices;
    private static final Logger LOGGER= LoggerFactory.getLogger(UpdateQuestionDb.class);

    public UpdateQuestionDb(QuestionResult questionResult, QuestionServices questionServices, AccountServices accountServices, AnswerServices answerServices) {

        this.questionResult = questionResult;
        this.questionServices = questionServices;
        this.accountServices = accountServices;
        this.answerServices = answerServices;
    }

    @Override
    public void run() {
        if(questionResult==null){
            return;
        }
        LOGGER.info("update DB: account:{},answer:{},question:{}",questionResult.getAccounts().size(),questionResult.getAnswers().size(),questionResult.getQuestions().size());
        accountServices.saveMySqlAccounts(new ArrayList<Account>(questionResult.getAccounts()));
        answerServices.saveAnswer(questionResult.getAnswers());
        questionServices.saveMysqlQuestions(questionResult.getQuestions());
    }
}
